package com.atguigu.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import javax.annotation.PostConstruct;

/**
 * @Author zhaoshuo
 * @ClassName RabbitmqConfig
 * @Description
 */
@Configuration
public class RabbitmqConfig {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            System.out.println("ConfirmCallback ack: " + ack);
            System.out.println("ConfirmCallback cause: " + cause);
        });

        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            System.out.println("ReturnCallback replyCode: " + replyCode);
            System.out.println("ReturnCallback replyText: " + replyText);
            System.out.println("ReturnCallback exchange: " + exchange);
            System.out.println("ReturnCallback routingKey: " + routingKey);
        });
    }


    @Bean
    public Exchange test1Exchange() {
        return ExchangeBuilder
                .topicExchange("test1.exchange")
                .durable(false)
                .ignoreDeclarationExceptions()
                .build();
    }


    @Bean
    public Queue test1Queue() {
        return QueueBuilder
                .nonDurable("test1.queue")
                .build();
    }


    @Bean
    public Binding test1Binding(Queue test1Queue, Exchange test1Exchange) {
        return BindingBuilder.bind(test1Queue).to(test1Exchange).with("a.*").noargs();
    }
}